package com.hrg.wms.web;


import com.hrg.wms.constant.LEDConstants;
import com.hrg.wms.domain.LedDefInfo;
import com.hrg.wms.domain.LedErrorInfo;
import com.hrg.wms.domain.LedTaskInfo;
import com.hrg.wms.domain.R;
import com.hrg.wms.service.LedShowService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StopWatch;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

/**
 * @author shenzq
 */
@Validated
@RequiredArgsConstructor
@RestController
@Slf4j
@RequestMapping("/wmsLed")
public class LedShowController {

    private final LedShowService ledShowService;

    /**
     * 显示正常出入库信息
     * @param ledTaskInfo
     * @return
     */
    @PostMapping("/showTaskInfo")
    public R<Void> showTaskInfo(@RequestBody LedTaskInfo ledTaskInfo) {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        if(ledTaskInfo.getInOutType().equals(LEDConstants.TASK_IN_YPE)){
            log.info("入库任务 showTaskInfo 入参：{}", ledTaskInfo);
        }
        if(ledTaskInfo.getInOutType().equals(LEDConstants.TASK_OUT_TYPE)){
            log.info("出库任务 showTaskInfo 入参：{}", ledTaskInfo);
        }
        try {
            this.ledShowService.showTaskInfo(ledTaskInfo);
        } catch (IOException e) {
            log.error("showTaskInfo 异常", e);
            R.fail(e.getMessage());
        }
        stopWatch.stop();
        log.info("showTaskInfo 耗时：{}", stopWatch.getTotalTimeMillis());
        return R.ok();
    }



    /**
     * 外形检查 不通过时，显示异常信息, 或者申请入口任务时，异常
     *               容器号：
     *           入口校验： 失败
     *           原因： 容器号不存在
     * @param ledErrorInfo
     * @return
     */
    @PostMapping("/showErrorInfo")
    public R<Void> showErrorInfo( @RequestBody LedErrorInfo ledErrorInfo) {
       StopWatch stopWatch = new StopWatch();
       stopWatch.start();
       log.info("showErrorInfo 入参：{}", ledErrorInfo);
        try {
            ledShowService.showErrorInfo(ledErrorInfo);
        } catch (IOException e) {
            log.error("showErrorInfo 异常", e);
            R.fail(e.getMessage());
        }
        stopWatch.stop();
        log.info("showErrorInfo 耗时：{}", stopWatch.getTotalTimeMillis());
        return R.ok();
    }

    /**
     * 没有任务时，显示的默认信息
     * @param ledDefInfo
     * @return
     */
    @PostMapping("/showDefInfo")
    public R<Void> showDefInfo(@RequestBody LedDefInfo ledDefInfo) {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        log.info("showDefInfo 入参：{}", ledDefInfo);
        try {
            ledShowService.showDefInfo(ledDefInfo);
        } catch (Exception e) {
            log.error("showDefInfo 异常", e);
            R.fail(e.getMessage());
        }
        stopWatch.stop();
        log.info("showDefInfo 耗时：{}", stopWatch.getTotalTimeMillis());
        return R.ok();
    }
}
